package com.hqyj.mall.xiaomimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.mall.xiaomimall.entity.Items;
import com.hqyj.mall.xiaomimall.entity.OrderMain;
import com.hqyj.mall.xiaomimall.entity.OrderSub;
import com.hqyj.mall.xiaomimall.mapper.OrderMainMapper;
import com.hqyj.mall.xiaomimall.service.OrderMainService;
import com.hqyj.mall.xiaomimall.service.OrderSubService;
import com.hqyj.mall.xiaomimall.util.MyUtil;
import com.hqyj.mall.xiaomimall.vo.Cart;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain> implements OrderMainService {

    @Resource
    private OrderMainMapper orderMainMapper;

    @Resource
    private OrderSubService orderSubService;

    @Transactional(rollbackOn = Exception.class)
    @Override
    public OrderMain generateNewOrder(Cart cart) throws Exception {
        OrderMain orderMain = new OrderMain();
        try{
            //分两步，首先生成订单主表，再保持订单子表；
            //1.生成一个订单编号；
            orderMain.setOid(MyUtil.generateOrderId());
            orderMain.setUid(cart.getUsers().getUid());
            orderMain.setRealname(cart.getAddress().getRealname());
            orderMain.setMobile(cart.getAddress().getMobile());
            orderMain.setProvince(cart.getAddress().getProvince());
            orderMain.setCity(cart.getAddress().getCity());
            orderMain.setZone(cart.getAddress().getZone());
            orderMain.setStreet(cart.getAddress().getStreet());
            orderMain.setTotalPrice(cart.getTotalPrice());

            if(super.save(orderMain)){
                //到这里表示订单主表已经生成了。
                //订单子表多条记录，你有多少件商品，有多少条订单子表的记录
                for(Items items: cart.getGoodsList()){
                    OrderSub orderSub = new OrderSub();
                    orderSub.setOid(orderMain.getOid());
                    orderSub.setItemId(items.getId());
                    orderSub.setName(items.getName());
                    orderSub.setPrice(items.getPrice());
                    orderSub.setBuyNum(items.getBuyNum());
                    orderSub.setSum(items.getPrice() * items.getBuyNum());
                    orderSubService.save(orderSub);
                }
            }

            return orderMain;

        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }

    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public OrderMain modifyPayStatus(String oid) throws Exception {
        try {
            OrderMain orderMain = super.getById(oid);
            orderMain.setIspay(true); //表示该订单已经支付;
            super.updateById(orderMain);

            return orderMain;
            //return false;
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
    }

    @Override
    public List<OrderMain> queryOrderMainListByUid(Integer uid) {
        QueryWrapper<OrderMain> queryWrapper = new QueryWrapper<OrderMain>();
        queryWrapper.eq("uid",uid);
        return super.list(queryWrapper);
    }
}
